package com.springmvc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.springmvc.dao.ItemsDao;
import com.springmvc.entity.Items;
import com.springmvc.entity.ItemsQueryVo;

/**
 * @author Administrator
 *
 */
@Repository
public class ItemsDaoImpl implements ItemsDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<Items> queryItemsListByCondition(ItemsQueryVo itemsQueryVo) {
		String sql = "select * from s_items s where 1=1";
		List<Object> queryList = new ArrayList<>();
		
		if (itemsQueryVo != null && itemsQueryVo.getItemsCustom() != null
				&& itemsQueryVo.getItemsCustom().getName() != null && itemsQueryVo.getItemsCustom().getName() != "") {
			sql += " and s.name like ?";
			queryList.add("%" + itemsQueryVo.getItemsCustom().getName() + "%");
		}
		RowMapper<Items> itemsMapper = new BeanPropertyRowMapper<>(Items.class);
		List<Items> list = jdbcTemplate.query(sql, itemsMapper, queryList.toArray());
		return list;
	}

	@Override
	public Items queryItemsByPrimaryKey(Integer id) {
		String sql = "select * from s_items where id = ?";
		RowMapper<Items> itemsMapper = new BeanPropertyRowMapper<>(Items.class);
		Items items = jdbcTemplate.queryForObject(sql, itemsMapper, id);
		return items;
	}

	@Override
	public int updateItemsByPrimaryKey(Items items) {
		String sql = "update s_items set name=?, price=?, detail=?, pic=?, createtime=? where id=?";
		Object[] args = {items.getName(), items.getPrice(), items.getDetail(), 
				items.getPic(), items.getCreatetime(),items.getId()};
		int i = jdbcTemplate.update(sql, args);
		return i;
	}

}
